import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/generated/json/base/json_convert_content.dart';
import 'package:library_base/entities/in_order_page_goods_details_entity.dart';
import 'package:library_base/entities/location_entity.dart';
import 'package:library_base/entities/reason_entity.dart';
import 'package:library_base/entities/warehouse_entity.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:library_base/network/http_util.dart';
import 'package:library_base/network/transform/fb_parse_transform.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/utils/get_util.dart';
import 'package:library_base/utils/picker/time_picker.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/utils/widget_generate_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/dialog/create_delivery_note_controller.dart';
import 'package:library_base/widget/image/local_image.dart';

class AsGoodsFilterDialog extends StatefulWidget {
  const AsGoodsFilterDialog({
    Key? key,
  }) : super(key: key);

  @override
  State<AsGoodsFilterDialog> createState() => _AsGoodsFilterDialogState();
}

class _AsGoodsFilterDialogState extends State<AsGoodsFilterDialog> {

  WarehouseEntity? warehouse;

  late Function() resetCallBack;

  late Function(WarehouseEntity warehouse) filterCallBack;

  var loginEnabled = false;

  @override
  void initState() {
    super.initState();
    warehouse = Get.arguments['warehouse'];
    resetCallBack = Get.arguments['reset'];
    filterCallBack = Get.arguments['filter'];
    loginEnabled = isCanSubmit();
  }
  
  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: WillPopScope(
          onWillPop: () {
        GetHelper.back();
        return Future.value(false);
      },
      child: GestureDetector(
      onTap: () => GetHelper.back(),
        child: Container(
          padding: const EdgeInsets.only(bottom: 50),
          width: double.infinity,
          color: Colors.transparent,
          alignment: Alignment.center,
          height: double.infinity,
          child: Row(
            children: [
              SizedBox(width: 0.06.sw),
              Expanded(
                child: Container(
                  decoration: rs8,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      SizedBox(
                        height: 95.w,
                        width: double.infinity,
                        child: Stack(
                          children: [
                            Container(
                                alignment: Alignment.center,
                                child: Text(MultiLanguage.orderFilter.tr,
                                    style: normalTextColor34)),
                            Positioned(
                              top: 10.w,
                              right: 0,
                              child: GestureDetector(
                                onTap: () => GetHelper.back(),
                                child: Padding(
                                    padding: edgeAll24,
                                    child: LocalImage('common_close',
                                        package: Constant.packLib(), width: 26.w, height: 26.w)),
                              ),
                            )
                          ],
                        ),
                      ),
                      gapH24,
                      line1,
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          gapH24,
                          buildItem(
                              onTap: chooseWarehouse,
                              title: MultiLanguage.warehouse.tr,
                              hint: MultiLanguage.pleaseSelectReceivingWarehouse.tr,
                              content: warehouse!=null?warehouse!.whName:""),
                          gapH24,
                        ],
                      ),
                      line1,
                      Container(
                        margin: edgeV36H24 as EdgeInsets,
                        child: Row(
                          children: [
                            Expanded(
                              child: GradientButton(
                                ThemeUtil.getTheme(),
                                radius: const BorderRadius.only(
                                    topLeft: Radius.circular(50), bottomLeft: Radius.circular(50)),
                                colors: ThemeUtil.getTheme().buttonLeftBgColor(),
                                height: 80.w,
                                text: MultiLanguage.reset.tr,
                                onPressed: reset,
                              ),
                            ),
                            Expanded(
                              child: GradientButton(
                                ThemeUtil.getTheme(),
                                radius: const BorderRadius.only(
                                    topRight: Radius.circular(50),
                                    bottomRight: Radius.circular(50)),
                                height: 80.w,
                                text: MultiLanguage.filter.tr,
                                onPressed: loginEnabled
                                    ? determine
                                    : null,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              SizedBox(width: 0.06.sw),
            ],
          ),
        ),
      )),
    );
  }

  buildItem(
      {GestureTapCallback? onTap, required String title, required String hint, String? content}) {
    return Row(children: [
      Expanded(
          flex: 2, child: Text(title, style: normalTextColor28, textAlign: TextAlign.end)),
      Gaps.hGap24,
      Expanded(
          flex: 7,
          child: GestureDetector(
            onTap: onTap,
            child: Container(
                padding: edgeAll24,
                decoration: BoxDecoration(
                    borderRadius: const BorderRadius.all(Radius.circular(30.0)),
                    border:
                    Border.all(color: ThemeUtil.getTheme().secondaryTextColor(), width: 1.0)),
                child: Row(children: [
                  Expanded(
                      child: Text(content == null || content.isEmpty ? hint : content,
                          style: content == null || content.isEmpty
                              ? TextStyles.textSecondary28
                              : normalTextColor28)),
                  LocalImage('common_grey_rightward_arrow',
                      package: Constant.packLib(), width: 20.w, height: 20.w),
                ])),
          )),
      gapW24,
    ]);
  }

  chooseWarehouse(){
    FBHttpUtil.get<Map>(context, Apis.WAREHOUSE,
        requestLoading: GenerateWidgetUtil.loadingDialog(context),
        transformer: FBParseTransform((result) => result))
    .then((value) async {
      if (value['code'] == HttpConfig.successCode) {
        var list = jsonConvert.convertList<WarehouseEntity>(value['data']);
        var callbackSuppliersEntity = await Get.toNamed(Routers.chooseWarehousePage,
            arguments: {'Warehouse': list, 'SelectedWarehouse': warehouse});
        if (callbackSuppliersEntity != null &&
            warehouse != callbackSuppliersEntity) {
          warehouse = callbackSuppliersEntity;
          setState(() {});
        }
      } else {
        ToastUtil.error(value['msg']);
      }
    }).catchError((err) {
      ToastUtil.error(err.toString());
    });
  }

  ///重置
  reset() {
    resetCallBack.call();
    GetHelper.back();
  }

  ///确定过滤
  determine() {
    if(warehouse == null){
      ToastUtil.waring(MultiLanguage.pleaseSelectReceivingWarehouse.tr);
      return;
    }
    filterCallBack.call(warehouse!);
    GetHelper.back();
  }

  ///判断是否自家的服务器标准的json格式,默认是有code和data即可
  bool isStandardJson(Map<dynamic, dynamic> data) {
    return data.containsKey('code') && data.containsKey('data') && data.containsKey('msg');
  }

  ///是否已经满足提交条件
  bool isCanSubmit(){
    return true;
  }

}
